package cn.easier.activity.console.quartz;

import cn.easier.activity.console.repository.ActivityRuleRepository;
import cn.easier.activity.console.repository.QuestionnaireMessageRepository;
import cn.easier.activity.web.entity.ActivityRule;
import cn.easier.activity.web.entity.QuestionnaireMessage;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.*;

@Component
public class DailyTimeTask {

    private final Logger log = LoggerFactory.getLogger(DailyTimeTask.class);
    private final QuestionnaireMessageRepository questionnaireMessageRepository;
    private final ActivityRuleRepository activityRuleRepository;

    public DailyTimeTask(QuestionnaireMessageRepository questionnaireMessageRepository, ActivityRuleRepository activityRuleRepository) {
        this.questionnaireMessageRepository = questionnaireMessageRepository;
        this.activityRuleRepository = activityRuleRepository;
    }


    /*
     *每天凌晨0点启动
     */
    @Scheduled(cron = "0 0 0 * * ?")
    private void growthTimeCheck() {
        try {
            log.info("================= 定时任务：每天循环一次,刷新题库,并且展示刷出来的题目 开始 =================");
            //得到所有每日问卷对应的活动ID
            Collection<Integer> activityIdList = questionnaireMessageRepository.findActivityId("daily");
            if (CollectionUtils.isEmpty(activityIdList)) {
                log.error("未查询到有任何相对应的活动ID");
                return;
            }
            List<ActivityRule> byActivityInfoIdIn = activityRuleRepository.findByActivityInfoIdIn(activityIdList);
            if (CollectionUtils.isEmpty(byActivityInfoIdIn)) {
                log.error("未查询到活动配置");
                return;
            }
            for (ActivityRule activityRule : byActivityInfoIdIn) {
                Integer countNum = activityRule.getCountNum();
                if (countNum == null || countNum == 0) {
                    log.error("请配置正确的每日刷新题目数量");
                    return;
                }
            }
            List<QuestionnaireMessage> byIsShowAndIsDel = questionnaireMessageRepository.findByIsShowAndIsDel(1, 0);

            //查出每日问卷都有哪些活动ID对应的问题
            List<QuestionnaireMessage> list = new ArrayList<>();
            List<QuestionnaireMessage> cancelList = new ArrayList<>();
            int i;
            //有几个每日问答活动就循环几次
            for (Integer integer : activityIdList) {
                list = questionnaireMessageRepository.findByActivityInfoIdAndIsDelAndIsShowAndYesterdayShow(integer, 0, 0, 0);
                cancelList = questionnaireMessageRepository.findByActivityInfoIdAndIsDelAndIsShowAndYesterdayShow(integer, 0, 1, 1);
                log.info("当前活动题库可用题目数量=>{},要取消的数量=>{}", list.size(), cancelList.size());
                Collections.shuffle(list);
                ActivityRule byActivityInfoId = activityRuleRepository.findByActivityInfoId(integer);
                Integer countNum = byActivityInfoId.getCountNum();
                if (countNum > list.size()) {
                    log.error("显示的题目超过了可用的最大的题目数量", integer);
                    continue;
                }
                log.info("每日刷新题库活动ID=>{},刷新数量=>{}", integer, countNum);
                for (i = 0; i < countNum; i++) {
                    QuestionnaireMessage questionnaireMessage1 = list.get(i);
                    questionnaireMessage1.setIsShow(1);
                    questionnaireMessage1.setYesterdayShow(1);
                    questionnaireMessageRepository.save(questionnaireMessage1);
                }
                for (QuestionnaireMessage message : cancelList) {
                	if(message.getIsRandom()==0) {//不随机
                          message.setIsDel(1);
                	}else {
                		   message.setYesterdayShow(0);
                           message.setIsShow(0);
                	}
                    questionnaireMessageRepository.save(message);
                }
            }
            log.info("================= 定时任务：每天循环一次,刷新题库,并且展示刷出来的题目 结束 =================");
        } catch (Exception e) {
            log.warn("================= 定时任务：每天循环一次,刷新题库,并且展示刷出来的题目 异常 =================", e);
        }
    }

}